<?php

class GroupController extends St_Controller_Action_Admin
{
    public function indexAction()
    {
		Zend_Loader::loadClass("St_Group");
		$groupObj = new St_Group ();
		$datas = $groupObj->fetchAll ();
		$this->view->datas = $datas;
                $this->view->baselink = St_Registry::getConfig("base_url");
		Zend_Loader::loadClass ( "Zend_Paginator_Adapter_DbSelect" );
		Zend_Loader::loadClass ( "Zend_Paginator" );
		$db = St_Registry::getDB ();
		$adapter = new Zend_Paginator_Adapter_DbSelect ( $db->select ()->from ( 'group' ) );
		$paginator = new Zend_Paginator ( $adapter );
		$paginator->setCurrentPageNumber ( $this->_getParam ( "page", 1 ) );
                $db = St_Registry::getDB ();
                
                $config = St_Registry::getConfig('display');                
		$paginator->setItemCountPerPage ( $config['numberPerPage'] );                
		$paginator->setPageRange ( $config['pageRange'] );
		$this->view->paginator = $paginator;
		$this->view->currentPage = $this->_getParam ( "page", 1 );
    }
    
    public function deleteAction()
    {
		Zend_Loader::loadClass("St_Group");
		$groupObj = new St_Group ();
		$gid = $this->_getParam ( "gid", false );
		
		$currentControllerName = St_Registry::getControllerName();
		if($gid == 1) {
			$this->_redirect ( "{$currentControllerName}/index/page/{$this->_getParam("page",1)}");
		} else {
			$groupObj->delete ( $groupObj->getAdapter ()->quoteInto ( "GroupID = ?", $gid ) );
                $user=$this->_getCurrentUser();
                $this->logger->log($user['nickName']." deletes GroupID =".$gid) ;
		}
		//redirect to other action
		$this->_redirect ( "{$currentControllerName}/index/page/{$this->_getParam("page",1)}");
    }
    
    public function activeAction()
    {
    	Zend_Loader::loadClass("St_Group");
		$groupObj = new St_Group ();
		$gid = $this->_getParam ( "gid", false );
		$status = $this->_getParam ( "currentstatus", "nostatus" );
		
		$currentControllerName = St_Registry::getControllerName();
		if($gid == 1 || $status == "nostatus") {
			$this->_redirect ( "{$currentControllerName}/index/page/{$this->_getParam("page",1)}");
		} else {
			
			$newStatus = ($status == "0")?"1":"0";
			
			$groupObj->update ( array('IsActive' => $newStatus), $groupObj->getAdapter ()->quoteInto ( "GroupID = ?", $gid ) );
                        $user=$this->_getCurrentUser();
                $this->logger->log($user['nickName']." actives GroupID =".$gid) ;
		}
		//redirect to other action
		$this->_redirect ( "{$currentControllerName}/index/page/{$this->_getParam("page",1)}");
    }

    public function addAction()
    {
        $data = $this->_getParam("data", false);
    	if($data) {
            Zend_Loader::loadClass("St_Group");
            $groupObj = new St_Group ();
            $data['IsActive'] = "1";
            $result = $groupObj->insert($data);
            
            if($result){
                $user=$this->_getCurrentUser();
                $this->logger->log($user['nickName']." adds a new Group") ;
                $this->view->msg = "Add new group successfully";
                $this->_redirect("group/index");
            } else {
                $this->view->msg = "Can't add group";
            }
    	}
        $this->view->groupMenuList = $this->_getMenuNameList();
    }

     public function editAction()
    {
        $gid = $this->_getParam("gid", false);
        if(!$gid) {
            $this->_redirect("group");
        }

    	$this->view->dataId = $gid;
    	Zend_Loader::loadClass("St_Group");
    	$groupObj = new St_Group();

        $arrayRow = $groupObj->fetchRow("GroupID = '{$gid}'");

    	if($arrayRow){
            $this->view->data = $arrayRow->toArray();
        }

        $data = $this->_getParam("data", false);
        
    	if($data) {
            $result = $groupObj->update($data, "GroupID = '$gid'");
            if($result){
                $user=$this->_getCurrentUser();
                $this->logger->log($user['nickName']." edits GroupID =".$gid) ;
                    $this->view->msg = "Update group successfully";
                    $this->_redirect("group/index");
            } else {
                    $this->view->msg = "Can't update group";
            }
            $this->view->data = $data;
    	}
        $this->view->groupMenuList = $this->_getMenuNameList();
    }

    
    
}

